package net.hangar5.xmlrpc;

/* RespWriter.java

The contents of this file are subject to the Mozilla Public
License Version 1.1 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
implied. See the License for the specific language governing
rights and limitations under the License.

The Original Code is "Hangar5 XMLRPC Library".

The Initial Developer of the Original Code is James D. Rudnicki.
Portions created by James D. Rudnicki are
Copyright (C) 2001.  All Rights Reserved.

Contributor(s):
*/
import java.util.*;

/** Converts call return value into an XML-RPC response.
 *
 */
public class RespWriter extends XmlWriter
{

  /** Exception writer
   */
  static public StringBuffer write( RpcException x )
  {
	StringBuffer sbResp = new StringBuffer(200);

	sbResp.append( "<?xml version=\"1.0\"?><methodResponse><fault><value>" );
	sbResp.append( "<struct><member><name>faultCode</name>" );
	writeValue( sbResp, "int", Integer.toString( x.getCode() ) );
	sbResp.append( "</member><member><name>faultString</name>" );
	writeValue( sbResp, "string", x.getMessage() );
	sbResp.append( "</member></struct>" );
	sbResp.append( "</value></fault></methodResponse>" );

	return sbResp;
  }
  /** Generate new response from object
   */
  static public StringBuffer writeObject( Object o )
  {
	StringBuffer sbResp = new StringBuffer(200);

	try {
	  sbResp.append( "<?xml version=\"1.0\"?><methodResponse><params><param>" );
	  writeObject( sbResp, o );
	  sbResp.append( "</param></params></methodResponse>" );
	}
	catch( RpcException x ) {
	  sbResp = write( x );
	}

	return sbResp;
  }
} // end of class
